package com.aaa.sys.aop;



import com.aaa.sys.entity.LoginLog;
import com.aaa.sys.service.LoginLogService;
import com.aaa.sys.util.ActivierUser;
import com.aaa.sys.vo.LoginVO;
import com.aaa.vo.R;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import io.swagger.models.auth.In;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.annotation.Pointcut;
import org.checkerframework.checker.units.qual.A;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.servlet.http.HttpServletRequest;
import java.util.Date;
import java.util.List;

@Aspect
@Component
public class LoginAop {
    private Object proceed;
    private R r;
    private LoginVO loginVO;


    @Autowired
    private LoginLogService loginLogService;


    @Pointcut("execution(* com.aaa.sys.controller.UserController.login(..))")
    public void pointLoginCut(){}

    @Pointcut("execution(* com.aaa.sys.controller.UserController.logout(..))")
    public void pointLoginOutCut(){}

    /*登录切面*/
    @Around("pointLoginCut()")
    public R pointLoginCut(ProceedingJoinPoint pjp) throws Throwable {
        HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder
                .getRequestAttributes()).getRequest();
        //获得方法执行后的返回值
        proceed = pjp.proceed();
        r= (R) proceed;
        Object[] args=pjp.getArgs();
        loginVO=(LoginVO) args[0];
        LoginLog loginLog=new LoginLog();
        if (r.getSuccess()){
            Object activeUser = r.getData().get("activeUser");
            ActivierUser activierUser=new ActivierUser();
            activierUser= (ActivierUser) activeUser;
            loginLog.setLogName("登录日志");
            loginLog.setUserId(activierUser.getUser().getUserId());
            Date now=new Date();
            loginLog.setCreateTime(now);
            loginLog.setIpAddress(request.getRemoteHost());
            loginLog.setSucceed("成功");
            loginLogService.save(loginLog);
        }else {
            loginLog.setLogName("登录失败日志");
            Date now=new Date();
            loginLog.setCreateTime(now);
            loginLog.setSucceed("失败");
            loginLog.setIpAddress(request.getRemoteHost());
            loginLog.setMessage("账号:"+loginVO.getUsername()+","+r.getMessage());
            loginLogService.save(loginLog);
        }
        return r;
    }
    /*登出切面*/
    @Around("pointLoginOutCut()")
    public R pointLoginOutCut(ProceedingJoinPoint pjp) throws Throwable {
        HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder
                .getRequestAttributes()).getRequest();
        //获得方法执行后的返回值
        proceed = pjp.proceed();
        r= (R) proceed;
        LoginLog loginLog=new LoginLog();
        loginLog.setLogName("退出日志");
        Date now=new Date();
        loginLog.setCreateTime(now);
        loginLog.setUserId((Long) r.getData().get("userid"));
        loginLog.setSucceed("成功");
        loginLog.setIpAddress(request.getRemoteHost());
        loginLogService.save(loginLog);
        return r;
    }
}
